package twelfth.对象序列化;

import java.io.*;

public class 序列化一组对象 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Person[] arrys = {
                new Person("王境泽", 18),
                new Person("小马云", 28),
                new Person("贵州科比", 38)};

        ser(arrys);
        Object[] o = dser();
        for (int i = 0; i <o.length ; i++) {
            Person  p = (Person) o[i];  //自我感觉是向下转型 所以需要cast。。
            System.out.println(p);
        }
    }

    public static Object[] dser() throws IOException, ClassNotFoundException {
        File file = new File("D:\\WorkSpaces\\JavaDevelopBase_start_329\\src\\twelfth\\序列化一组对象.txt");
        InputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object obj[] = (Object[]) ois.readObject();
        ois.close();
        return obj;
    }

    public static void ser(Object[] obj) throws IOException {
        File file = new File("D:\\WorkSpaces\\JavaDevelopBase_start_329\\src\\twelfth\\序列化一组对象.txt");
        OutputStream os = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(obj);
        oos.close();
    }
}
/**
 *
 */
/*
D:\WorkSpaces\Java\jdk-21\bin\java.exe "-javaagent:D:\Software\IntelliJ IDEA\IntelliJ IDEA 2023.3.2\lib\idea_rt.jar=57213:D:\Software\IntelliJ IDEA\IntelliJ IDEA 2023.3.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\WorkSpaces\JavaDevelopBase_start_329\out\production\JavaDevelopBase_start_329 twelfth.对象序列化.序列化一组对象
Person{name='王境泽', age=18}
Person{name='小马云', age=28}
Person{name='贵州科比', age=38}

Process finished with exit code 0
 */